function rgb = HsvToRgb (hue, saturation, lightness)

if hue < 0
    hue = 0;
else
    if hue > 1;
        hue = 1;
    end
end

if saturation < 0
    saturation = 0;
else
    if saturation > 1;
        saturation = 1;
    end
end

if lightness < 0
    lightness = 0;
else
    if lightness > 1;
        lightness = 1;
    end
end


white = [1;1;1];

red = [1;0;0];
yellow = [1;1;0];
green= [0;1;0];
cyan = [0;1;1];
blue = [0;0;1];
violet = [1;0;1];

nColors = 6;

if hue < 1/nColors
    rgb = red * (1-hue*nColors) + yellow * hue*nColors;  %red yellow
else if hue < 2/nColors
        hue = hue - 1/nColors;
        rgb = yellow * (1-hue*nColors) + green * hue*nColors; % yellow green
    else if hue < 3/nColors
            hue = hue - 2/nColors;
            rgb = green * (1-hue*nColors) + cyan * hue*nColors; % green cyan
        else if hue < 4/nColors;
                hue = hue - 3/nColors;
                rgb = cyan * (1-hue*nColors) + blue * hue*nColors; % cyan blue
            else if hue < 5/nColors
                    hue = hue - 4/nColors;
                    rgb = blue * (1-hue*nColors) + violet * hue*nColors; % blue violet
                else
                    hue = hue - 5/nColors;
                    rgb = violet * (1-hue*nColors) + red * hue*nColors; % blue red
                end
            end
        end
    end
end
rgb= rgb.*saturation + white.*(1-saturation);
rgb= rgb.*lightness;


